package org.ben.gather;

import java.util.*;


/**
 * @Author: Ben
 * @Date: 2019/10/16 17:32
 */
public class ListTest {
    public static void main(String[] args) {
        List<String> listString=new ArrayList<>();
        listString.add("Java");
        listString.add("Pyhton");
        listString.add("C");
        listString.add("C++");
        listString.add("Matlab");
        listString.add("R");
        listString.add("html");
        listString.add("CSS");
        listString.add("JavaScript");
        ListTest listTest=new ListTest();
        listTest.forStringList(listString);
        System.out.println("-----------------------------------");
        List<Integer> integerList=new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            integerList.add(i);
        }
        listTest.forEachIntegerList(integerList);
        System.out.println("-----------------------------------");
        listTest.IteratorRemoveList(integerList);
        System.out.println("-----------------------------------");
        integerList.remove(0);
        listTest.forEachIntegerList(integerList);
        List<User> userList=new ArrayList<>();
        userList.add(new User(1,"mjz","mjz","mjz","mjz"));
        userList.add(new User(2,"aaa","aaa","aaa","aaa"));
        userList.add(new User(3,"bbb","bbb","bbb","bbb"));
        userList.add(new User(4,"ccc","ccc","ccc","ccc"));
        userList.add(new User(5,"ddd","ddd","ddd","ddd"));
        userList.add(new User(6,"eee","eee","eee","eee"));
        userList.add(new User(7,"fff" ,"fff","fff","fff"));
        listTest.lambdaUserList(userList);
        System.out.println("-----------------------------------");
        listTest.IteratorUserList(userList);
        userList.set(0,new User(8,"ggg" ,"ggg","ggg","ggg"));
        System.out.println("===========================");
        listTest.IteratorlambdaUserList(userList);
        System.out.println("==========----------------------------------");
        listTest.compareUserList(userList);
    }

    /**
     * 用for循环迭代List<String>集合
     * @param list
     */
    public void forStringList(List<String> list){
        for (int i = 0; i <list.size() ; i++) {
            System.out.println(list.get(i));
        }
    }

    /**
     * 用foreach循环迭代List<Integer>集合
     * @param list
     */
    public void forEachIntegerList(List<Integer> list){
        for (Integer a:list) {
            System.out.println(a);
        }
    }

    /**
     * 用foreach的lambda表达式迭代List<User>集合
     * @param list
     */
    public void lambdaUserList(List<User> list){
        list.forEach(obj -> System.out.println(obj));
    }

    /**
     * 使用迭代器的删除方法
     * @param list
     */
    public void IteratorRemoveList(List<Integer> list){
        Iterator<Integer> integerIterator=list.iterator();
        while(integerIterator.hasNext()){
           Integer returnInteger=integerIterator.next();
            if (returnInteger==1){
                integerIterator.remove();
            }
        }
      forEachIntegerList(list);
    }
    /**
     * 用Iterator迭代器迭代List<User>集合
     * @param list
     */
    public void IteratorUserList(List<User> list){
        Iterator<User> iterator=list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
    }

    /**
     *用Iterator迭代器的forEachRemaining方法运用lambda表达式迭代List<User>集合
     * @param list
     */
    public void IteratorlambdaUserList(List<User> list){
        Iterator<User> iterator=list.iterator();
        iterator.forEachRemaining(Object-> System.out.println("迭代集合元素"+Object));
    }

    /**
     * 使用排序器对List集合进行排序
     * @param list
     */
    public void compareUserList(List<User> list){
    Collections.sort(list,new Comparator<User>(){
        @Override
        public int compare(User user1, User user2) {
            if (user1.getId() > user2.getId()){
                return -1;
            }else if (user1.getId() == user2.getId()){
                return 0;
            }
            return 1;
        }
    });
    for (User user:list) {
        System.out.println(user.toString());
    }
}
}
